package com.cqupt.scanning.system.consumer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.*;
import com.cqupt.scanning.system.admin.entity.dto.PatientFootDTO;
import com.cqupt.scanning.system.consumer.mapper.ConsumerMapper;
import com.cqupt.scanning.system.admin.service.DeviceFootService;
import com.cqupt.scanning.system.admin.service.DeviceThreeService;
import com.cqupt.scanning.system.admin.service.PatientFootService;
import com.cqupt.scanning.system.admin.service.PatientThreeService;
import com.cqupt.scanning.system.consumer.service.ConsumerService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.BaseStream;

/**
 * @Data: 2021/7/12 16:01
 * @Author: 宋宝梁
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, PatientThree> implements ConsumerService {

    private AclUserService aclUserService;
    private DeviceFootService deviceFootService;
    private DeviceThreeService deviceThreeService;
    private PatientFootService patientFootService;
    private PatientThreeService patientThreeService;

    @Autowired
    public ConsumerServiceImpl(AclUserService aclUserService, DeviceFootService deviceFootService,
                               DeviceThreeService deviceThreeService, PatientFootService patientFootService,
                               PatientThreeService patientThreeService) {
        this.aclUserService = aclUserService;
        this.deviceFootService = deviceFootService;
        this.deviceThreeService = deviceThreeService;
        this.patientFootService = patientFootService;
        this.patientThreeService = patientThreeService;
    }

    @Override
    public R getAllPatsCount() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));

        int patcounts = 0;
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            if (!StrUtil.hasEmpty(deviceFoot.getDevNum())) {
                patcounts = patientFootService.count(new QueryWrapper<PatientFoot>().eq("dev_num", deviceFoot.getDevNum()));
            }
        }

        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            if (!StrUtil.hasEmpty(deviceThree.getDevNum())) {
                patcounts = patientThreeService.count(new QueryWrapper<PatientThree>().eq("dev_num", deviceThree.getDevNum()));
            }
        }
        return R.ok().data("counts", patcounts);
    }

    @Override
    public R getAllPats(String query, Integer current, Integer size) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            Page<PatientFoot> page = new Page<>(current, size);
            IPage<PatientFoot> patFootPage = patientFootService.page(page, new QueryWrapper<PatientFoot>().eq("dev_num", devNum).like("name", query));
            List<PatientFoot> records = patFootPage.getRecords();
            List<PatientFootDTO> patientsFoots = new ArrayList<>();
            for (PatientFoot record : records) {
                PatientFootDTO patientFootDTO = new PatientFootDTO();
                BeanUtils.copyProperties(record, patientFootDTO);
                patientsFoots.add(patientFootDTO);
            }
            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
        }
        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceThree.getDevNum();
            Page<PatientThree> page = new Page<>(current, size);

            IPage<PatientThree> patThreePage = patientThreeService.page(page, new QueryWrapper<PatientThree>().eq("dev_num", devNum).like("name", query));
            List<PatientThree> records = patThreePage.getRecords();
            List<PatientThree> patientsThrees = new ArrayList<>();
            for (PatientThree record : records) {
                PatientThree patientThree = new PatientThree();
                BeanUtils.copyProperties(record, patientThree);
                patientsThrees.add(patientThree);
                return R.ok().data("pats", patThreePage).data("total",patThreePage.getTotal());
//
//            IPage<PatientThree> patThreePage = patientThreeService.page(page, new QueryWrapper<PatientThree>()
//                    .select("id","name","sex","testTime","file_path")
//                    .like("name", query).eq("dev_num", devNum));
//                return R.ok().data("pats", patThreePage);
            }
        }
            return R.error();

    }

    @Override
    public R queryAllPats(String name, String ageLower,String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper,String phone,String beginTime,String endTime,Integer current, Integer size) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));

        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            Page<PatientFoot> page = new Page<>(current, size);
            QueryWrapper<PatientFoot> w = new QueryWrapper<>();
            if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
                w.ge("age", ageLower).le("age", ageUpper);
            }
            if (!StrUtil.isEmpty(doctor)){
                w.like("doctor",doctor);
            }
            if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
                w.ge("height",heightLower).le("height",heightUpper);
            }
            if (!StrUtil.isEmpty(measureMode)){
                w.like("gaitMeasureMode",measureMode);
            }
            if (!StrUtil.isEmpty(sex)){
                w.eq("sex", sex);
            }
            if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
                w.ge("weight",weightLower).le("weight",weightUpper);
            }
            if(!StrUtil.isEmpty(phone)){
                w.eq("phone",phone);
            }
            if(!StrUtil.isEmpty(name)) {
                w.like("name",name);
            }
            if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
                w.ge("create_time",beginTime).le("create_time",endTime);
            }
            IPage<PatientFoot> patFootPage = patientFootService.page(page, w.eq("dev_num",devNum));
            List<PatientFoot> records = patFootPage.getRecords();
            List<PatientFootDTO> patientsFoots = new ArrayList<>();
            for (PatientFoot record : records) {
                PatientFootDTO patientFootDTO = new PatientFootDTO();
                BeanUtils.copyProperties(record, patientFootDTO);
                patientsFoots.add(patientFootDTO);
            }
            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
        }
        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceThree.getDevNum();
            Page<PatientThree> page = new Page<>(current, size);
            QueryWrapper<PatientThree> w = new QueryWrapper<>();
            if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
                w.ge("age", ageLower).le("age", ageUpper);
            }
            if (!StrUtil.isEmpty(doctor)){
                w.like("doctor",doctor);
            }
            if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
                w.ge("height",heightLower).le("height",heightUpper);
            }
            if (!StrUtil.isEmpty(measureMode)){
                w.like("gaitMeasureMode",measureMode);
            }
            if (!StrUtil.isEmpty(sex)){
                w.eq("sex", sex);
            }
            if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
                w.ge("weight",weightLower).le("weight",weightUpper);
            }
            if(!StrUtil.isEmpty(phone)){

                w.eq("phone",phone);
            }
            if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
                w.ge("create_time",beginTime).le("create_time",endTime);
            }
            if(!StrUtil.isEmpty(name)) {
                w.like("name",name);
            }
//            IPage<PatientThree> patThreePage = patientThreeService.page(page, w.select("id","name","sex","testTime","file_path").eq("dev_num",devNum));
            IPage<PatientThree> patThreePage = patientThreeService.page(page, w.eq("dev_num",devNum));
            List<PatientThree> records = patThreePage.getRecords();
            List<PatientThree> patientsThrees = new ArrayList<>();
            for (PatientThree record : records) {
                PatientThree patientThreeDTO = new PatientThree();
                BeanUtils.copyProperties(record, patientThreeDTO);
                patientsThrees.add(patientThreeDTO);
            }
            return R.ok().data("threes", patientsThrees).data("total", patThreePage.getTotal());
        }

        return R.error();
    }

//    @Override
//    public R queryAllByTime(String beginTime, String endTime,Integer current, Integer size) {
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
//        if (aclUser.getType().equals("步态分析")) {
//            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
//            String devNum = deviceFoot.getDevNum();
//            Page<PatientFoot> page = new Page<>(current, size);
//            IPage<PatientFoot> patFootPage = patientFootService.page(page, new QueryWrapper<PatientFoot>().eq("dev_num", devNum).ge("create_time", beginTime).le("create_time", endTime));
//            List<PatientFoot> records = patFootPage.getRecords();
//            List<PatientFootDTO> patientsFoots = new ArrayList<>();
//            for (PatientFoot record : records) {
//                PatientFootDTO patientFootDTO = new PatientFootDTO();
//                BeanUtils.copyProperties(record, patientFootDTO);
//                patientsFoots.add(patientFootDTO);
//            }
//            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
//        }
//        if (aclUser.getType().equals("三维扫描")) {
//            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
//            String devNum = deviceThree.getDevNum();
//            Page<PatientThree> page = new Page<>(current, size);
//            IPage<PatientThree> patThreePage = patientThreeService.page(page, new QueryWrapper<PatientThree>()
//                    .select("id","name","sex","testTime","file_path")
//                    .ge("create_time", beginTime).le("create_time", endTime).eq("dev_num", devNum));
//            return R.ok().data("pats", patThreePage);
//        }
//
//        return R.error();
//    }


    @Override
    public R queryAllThrees(Page<PatientThree> page, String name, String ageLower, String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper, String beginTime, String endTime, String phone, String orgName, String devNum, String businessName, String address, String cityProvince, String country, String fax, String weightBearingLower, String weightBearingUpper, String dressShoe, String athleticShoe, String casualShoe, String heels)
    {
        QueryWrapper<PatientThree> w = new QueryWrapper<>();
        if (!StrUtil.isEmpty(name)){
            w.like("name",name);
        }
        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
            w.ge("age", ageLower).le("age", ageUpper);
        }
        if (!StrUtil.isEmpty(doctor)){
            w.like("doctor",doctor);
        }
        if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
            w.ge("height",heightLower).le("height",heightUpper);
        }
        if (!StrUtil.isEmpty(measureMode)){
            w.like("gaitMeasureMode",measureMode);
        }
        if (!StrUtil.isEmpty(sex)){
            w.eq("sex", sex);
        }
        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
            w.ge("weight",weightLower).le("weight",weightUpper);
        }
        if (!StrUtil.isEmpty(orgName)){
            w.like("org_name",orgName);
        }
        if (!StrUtil.isEmpty(devNum)){
            w.eq("dev_num",devNum);
        }
        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
            w.ge("create_time",beginTime).le("create_time",endTime);
        }
        if(!StrUtil.isEmpty(phone)){
            w.eq("phone",phone);
        }
        if(!StrUtil.isEmpty(businessName)){
            w.like("business_name",businessName);
        }
        if(!StrUtil.isEmpty(address)){
            w.like("address",address);
        }
        if(!StrUtil.isEmpty(cityProvince)){
            w.like("city_province",cityProvince);
        }
        if(!StrUtil.isEmpty(country)){
            w.like("country",country);
        }
        if(!StrUtil.isEmpty(fax)){
            w.eq("fax",fax);
        }
        if (!StrUtil.isEmpty(weightBearingLower) && !StrUtil.isEmpty(weightBearingUpper)){
            w.ge("weight_bearing",weightBearingLower).le("weight_bearing",weightBearingUpper);
        }
        if(!StrUtil.isEmpty(dressShoe)){
            w.like("dress_shoe",dressShoe);
        }
        if(!StrUtil.isEmpty(athleticShoe)){
            w.like("athletic_shoe",athleticShoe);
        }
        if(!StrUtil.isEmpty(casualShoe)){
            w.like("casual_shoe",casualShoe);
        }
        if(!StrUtil.isEmpty(heels)){
            w.like("heels",heels);
        }

        IPage<PatientThree> pathThreePage = baseMapper.selectPage(page, w);//根据 w(可为null)的条件，查询全部记录（并翻页）
        List<PatientThree> records = pathThreePage.getRecords();
        List<PatientThree> patientsThrees = new ArrayList<>();
        for (PatientThree record : records) {
            PatientThree patientThree = new PatientThree();
            BeanUtils.copyProperties(record, patientThree);
            patientsThrees.add(patientThree);
        }

        System.out.println("第"+patientsThrees+"页，本次查询共有条数为"+pathThreePage.getTotal());
        return R.ok().data("pats", patientsThrees).data("total", pathThreePage.getTotal());



    }

//    @Override
//    public R queryAllThreesBearing(Page<PatientThreeBearing> page, String name, String ageLower, String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper, String beginTime, String endTime, String phone, String orgName, String devNum, String businessName, String address, String cityProvince, String country, String fax, String weightBearingLower, String weightBearingUpper, String dressShoe, String athleticShoe, String casualShoe, String heels)
//    {
//        QueryWrapper<PatientThreeBearing> w = new QueryWrapper<>();
//        if (!StrUtil.isEmpty(name)){
//            w.like("name",name);
//        }
//        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
//            w.ge("age", ageLower).le("age", ageUpper);
//        }
//        if (!StrUtil.isEmpty(doctor)){
//            w.like("doctor",doctor);
//        }
//        if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
//            w.ge("height",heightLower).le("height",heightUpper);
//        }
//        if (!StrUtil.isEmpty(measureMode)){
//            w.like("gaitMeasureMode",measureMode);
//        }
//        if (!StrUtil.isEmpty(sex)){
//            w.eq("sex", sex);
//        }
//        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
//            w.ge("weight",weightLower).le("weight",weightUpper);
//        }
//        if (!StrUtil.isEmpty(orgName)){
//            w.like("org_name",orgName);
//        }
//        if (!StrUtil.isEmpty(devNum)){
//            w.eq("dev_num",devNum);
//        }
//        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
//            w.ge("create_time",beginTime).le("create_time",endTime);
//        }
//        if(!StrUtil.isEmpty(phone)){
//            w.eq("phone",phone);
//        }
//        if(!StrUtil.isEmpty(businessName)){
//            w.like("business_name",businessName);
//        }
//        if(!StrUtil.isEmpty(address)){
//            w.like("address",address);
//        }
//        if(!StrUtil.isEmpty(cityProvince)){
//            w.like("city_province",cityProvince);
//        }
//        if(!StrUtil.isEmpty(country)){
//            w.like("country",country);
//        }
//        if(!StrUtil.isEmpty(fax)){
//            w.eq("fax",fax);
//        }
//        if (!StrUtil.isEmpty(weightBearingLower) && !StrUtil.isEmpty(weightBearingUpper)){
//            w.ge("weight_bearing",weightBearingLower).le("weight_bearing",weightBearingUpper);
//        }
//        if(!StrUtil.isEmpty(dressShoe)){
//            w.like("dress_shoe",dressShoe);
//        }
//        if(!StrUtil.isEmpty(athleticShoe)){
//            w.like("athletic_shoe",athleticShoe);
//        }
//        if(!StrUtil.isEmpty(casualShoe)){
//            w.like("casual_shoe",casualShoe);
//        }
//        if(!StrUtil.isEmpty(heels)){
//            w.like("heels",heels);
//        }
//
//        IPage<PatientThreeBearing> pathThreeBearingPage = baseMapper.selectPage(page, w);//根据 w(可为null)的条件，查询全部记录（并翻页）
//        List<PatientThreeBearing> records = pathThreeBearingPage.getRecords();
//        List<PatientThreeBearing> patientsThreesBearing = new ArrayList<>();
//        for (PatientThreeBearing record : records) {
//            PatientThreeBearing patientThreeBearing = new PatientThreeBearing();
//            BeanUtils.copyProperties(record, patientThreeBearing);
//            patientsThreesBearing.add(patientThreeBearing);
//        }
//
//        System.out.println("第"+patientsThreesBearing+"页，本次查询共有条数为"+pathThreeBearingPage.getTotal());
//        return R.ok().data("pats", patientsThreesBearing).data("total", pathThreeBearingPage.getTotal());
//
//
//
//    }
}